home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / samples / wave.c.z / wave.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  13.6 KB  |  576 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "tk.h"
  6.  
  7.  
  8. #define PI 3.14159265358979323846
  9.  
  10. #define GETCOORD(frame, x, y) (&(theMesh.coords[frame*theMesh.numCoords+(x)+(y)*(theMesh.widthX+1)]))
  11. #define GETFACET(frame, x, y) (&(theMesh.facets[frame*theMesh.numFacets+(x)+(y)*theMesh.widthX]))
  12.  
  13.  
  14. GLenum rgb, doubleBuffer, directRender;
  15.  
  16. GLint colorIndexes1[3];
  17. GLint colorIndexes2[3];
  18. GLenum clearMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
  19.  
  20. GLenum smooth = GL_FALSE;
  21. GLenum lighting = GL_TRUE;
  22. GLenum depth = GL_TRUE;
  23. GLenum stepMode = GL_FALSE;
  24. GLenum spinMode = GL_FALSE;
  25. GLint contouring = 0;
  26.  
  27. GLint widthX, widthY;
  28. GLint checkerSize;
  29. float height;
  30.  
  31. GLint frames, curFrame = 0, nextFrame = 0;
  32.  
  33. struct facet {
  34.     float color[3];
  35.     float normal[3];
  36. };
  37. struct coord {
  38.     float vertex[3];
  39.     float normal[3];
  40. };
  41. struct mesh {
  42.     GLint widthX, widthY;
  43.     GLint numFacets;
  44.     GLint numCoords;
  45.     GLint frames;
  46.     struct coord *coords;
  47.     struct facet *facets;
  48. } theMesh;
  49.  
  50. GLubyte contourTexture1[] = {
  51.     255, 255, 255, 255,
  52.     255, 255, 255, 255,
  53.     255, 255, 255, 255,
  54.     127, 127, 127, 127,
  55. };
  56. GLubyte contourTexture2[] = {
  57.     255, 255, 255, 255,
  58.     255, 127, 127, 127,
  59.     255, 127, 127, 127,
  60.     255, 127, 127, 127,
  61. };
  62.  
  63.  
  64. static void Animate(void)
  65. {
  66.     struct coord *coord;
  67.     struct facet *facet;
  68.     float *lastColor;
  69.     float *thisColor;
  70.     GLint i, j;
  71.  
  72.     glClear(clearMask);
  73.  
  74.     if (nextFrame || !stepMode) {
  75.     curFrame++;
  76.     }
  77.     if (curFrame >= theMesh.frames) {
  78.     curFrame = 0;
  79.     }
  80.  
  81.     if ((nextFrame || !stepMode) && spinMode) {
  82.     glRotatef(5.0, 0.0, 0.0, 1.0);
  83.     }
  84.     nextFrame = 0;
  85.  
  86.     for (i = 0; i < theMesh.widthX; i++) {
  87.     glBegin(GL_QUAD_STRIP);
  88.     lastColor = NULL;
  89.     for (j = 0; j < theMesh.widthY; j++) {
  90.         facet = GETFACET(curFrame, i, j);
  91.         if (!smooth && lighting) {
  92.         glNormal3fv(facet->normal);
  93.         }
  94.         if (lighting) {
  95.         if (rgb) {
  96.             thisColor = facet->color;
  97.             glColor3fv(facet->color);
  98.         } else {
  99.             thisColor = facet->color;
  100.             glMaterialfv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES, 
  101.                  facet->color);
  102.         }
  103.         } else {
  104.         if (rgb) {
  105.             thisColor = facet->color;
  106.             glColor3fv(facet->color);
  107.         } else {
  108.             thisColor = facet->color;
  109.             glIndexf(facet->color[1]);
  110.         }
  111.         }
  112.  
  113.         if (!lastColor || (thisColor[0] != lastColor[0] && smooth)) {
  114.         if (lastColor) {
  115.             glEnd();
  116.             glBegin(GL_QUAD_STRIP);
  117.         }
  118.         coord = GETCOORD(curFrame, i, j);
  119.         if (smooth && lighting) {
  120.             glNormal3fv(coord->normal);
  121.         }
  122.         glVertex3fv(coord->vertex);
  123.  
  124.         coord = GETCOORD(curFrame, i+1, j);
  125.         if (smooth && lighting) {
  126.             glNormal3fv(coord->normal);
  127.         }
  128.         glVertex3fv(coord->vertex);
  129.         }
  130.  
  131.         coord = GETCOORD(curFrame, i, j+1);
  132.         if (smooth && lighting) {
  133.         glNormal3fv(coord->normal);
  134.         }
  135.         glVertex3fv(coord->vertex);
  136.  
  137.         coord = GETCOORD(curFrame, i+1, j+1);
  138.         if (smooth && lighting) {
  139.         glNormal3fv(coord->normal);
  140.         }
  141.         glVertex3fv(coord->vertex);
  142.  
  143.         lastColor = thisColor;
  144.     }
  145.     glEnd();
  146.     }
  147.  
  148.     glFlush();
  149.     if (doubleBuffer) {
  150.     tkSwapBuffers();
  151.     }
  152. }
  153.  
  154. static void SetColorMap(void) 
  155. {
  156.     static float green[3] = {0.2, 1.0, 0.2};
  157.     static float red[3] = {1.0, 0.2, 0.2};
  158.     float *color, percent;
  159.     GLint *indexes, entries, i, j;
  160.     long buf[4];
  161.  
  162.     entries = tkGetColorMapSize();
  163.  
  164.     colorIndexes1[0] = 1;
  165.     colorIndexes1[1] = 1 + (GLint)((entries - 1) * 0.3);
  166.     colorIndexes1[2] = (GLint)((entries - 1) * 0.5);
  167.     colorIndexes2[0] = 1 + (GLint)((entries - 1) * 0.5);
  168.     colorIndexes2[1] = 1 + (GLint)((entries - 1) * 0.8);
  169.     colorIndexes2[2] = entries - 1;
  170.  
  171.     for (i = 0; i < 2; i++) {
  172.     switch (i) {
  173.       case 0:
  174.         color = green;
  175.         indexes = colorIndexes1;
  176.         break;
  177.       case 1:
  178.         color = red;
  179.         indexes = colorIndexes2;
  180.         break;
  181.     }
  182.  
  183.     for (j = indexes[0]; j < indexes[1]; j++) {
  184.         percent = 0.2 + 0.8 * (j - indexes[0]) /
  185.               (float)(indexes[1] - indexes[0]);
  186.         tkSetOneColor(j, percent*color[0], percent*color[1],
  187.                percent*color[2]);
  188.     }
  189.     for (j=indexes[1]; j<=indexes[2]; j++) {
  190.         percent = (j - indexes[1]) / (float)(indexes[2] - indexes[1]);
  191.         tkSetOneColor(j, percent*(1-color[0])+color[0],
  192.                percent*(1-color[1])+color[1],
  193.                percent*(1-color[2])+color[2]);
  194.     }
  195.     }
  196. }
  197.  
  198. static void InitMesh(void)
  199. {
  200.     struct coord *coord;
  201.     struct facet *facet;
  202.     float dp1[3], dp2[3];
  203.     float *pt1, *pt2, *pt3;
  204.     float angle, d, x, y;
  205.     GLint numFacets, numCoords, frameNum, i, j;
  206.  
  207.     theMesh.widthX = widthX;
  208.     theMesh.widthY = widthY;
  209.     theMesh.frames = frames;
  210.  
  211.     numFacets = widthX * widthY;
  212.     numCoords = (widthX + 1) * (widthY + 1);
  213.  
  214.     theMesh.numCoords = numCoords;
  215.     theMesh.numFacets = numFacets;
  216.  
  217.     theMesh.coords = (struct coord *)malloc(frames*numCoords*
  218.                         sizeof(struct coord));
  219.     theMesh.facets = (struct facet *)malloc(frames*numFacets*
  220.                         sizeof(struct facet));
  221.     if (theMesh.coords == NULL || theMesh.facets == NULL) {
  222.     printf("Out of memory.\n");
  223.     tkQuit();
  224.     }
  225.  
  226.     for (frameNum = 0; frameNum < frames; frameNum++) {
  227.     for (i = 0; i <= widthX; i++) {
  228.         x = i / (float)widthX;
  229.         for (j = 0; j <= widthY; j++) {
  230.         y = j / (float)widthY;
  231.  
  232.         d = sqrt(x*x+y*y);
  233.         if (d == 0.0) {
  234.             d = 0.0001;
  235.         }
  236.         angle = 2 * PI * d + (2 * PI / frames * frameNum);
  237.  
  238.         coord = GETCOORD(frameNum, i, j);
  239.  
  240.         coord->vertex[0] = x - 0.5;
  241.         coord->vertex[1] = y - 0.5;
  242.         coord->vertex[2] = (height - height * d) * cos(angle);
  243.  
  244.         coord->normal[0] = -(height / d) * x * ((1 - d) * 2 * PI *
  245.                    sin(angle) + cos(angle));
  246.         coord->normal[1] = -(height / d) * y * ((1 - d) * 2 * PI *
  247.                    sin(angle) + cos(angle));
  248.         coord->normal[2] = -1;
  249.  
  250.         d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
  251.                    coord->normal[1]*coord->normal[1]+1);
  252.         coord->normal[0] *= d;
  253.         coord->normal[1] *= d;
  254.         coord->normal[2] *= d;
  255.         }
  256.     }
  257.     for (i = 0; i < widthX; i++) {
  258.         for (j = 0; j < widthY; j++) {
  259.         facet = GETFACET(frameNum, i, j);
  260.         if (((i/checkerSize)%2)^(j/checkerSize)%2) {
  261.             if (rgb) {
  262.             facet->color[0] = 1.0;
  263.             facet->color[1] = 0.2;
  264.             facet->color[2] = 0.2;
  265.             } else {
  266.             facet->color[0] = colorIndexes1[0];
  267.             facet->color[1] = colorIndexes1[1];
  268.             facet->color[2] = colorIndexes1[2];
  269.             }
  270.         } else {
  271.             if (rgb) {
  272.             facet->color[0] = 0.2;
  273.             facet->color[1] = 1.0;
  274.             facet->color[2] = 0.2;
  275.             } else {
  276.             facet->color[0] = colorIndexes2[0];
  277.             facet->color[1] = colorIndexes2[1];
  278.             facet->color[2] = colorIndexes2[2];
  279.             }
  280.         }
  281.         pt1 = GETCOORD(frameNum, i, j)->vertex;
  282.         pt2 = GETCOORD(frameNum, i, j+1)->vertex;
  283.         pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
  284.  
  285.         dp1[0] = pt2[0] - pt1[0];
  286.         dp1[1] = pt2[1] - pt1[1];
  287.         dp1[2] = pt2[2] - pt1[2];
  288.  
  289.         dp2[0] = pt3[0] - pt2[0];
  290.         dp2[1] = pt3[1] - pt2[1];
  291.         dp2[2] = pt3[2] - pt2[2];
  292.  
  293.         facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
  294.         facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
  295.         facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
  296.  
  297.         d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
  298.                    facet->normal[1]*facet->normal[1]+
  299.                    facet->normal[2]*facet->normal[2]);
  300.  
  301.         facet->normal[0] *= d;
  302.         facet->normal[1] *= d;
  303.         facet->normal[2] *= d;
  304.         }
  305.     }
  306.     }
  307. }
  308.  
  309. static void InitMaterials(void)
  310. {
  311.     static float ambient[] = {0.1, 0.1, 0.1, 1.0};
  312.     static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
  313.     static float position[] = {90.0, 90.0, 150.0, 0.0};
  314.     static float front_mat_shininess[] = {60.0};
  315.     static float front_mat_specular[] = {0.2, 0.2, 0.2, 1.0};
  316.     static float front_mat_diffuse[] = {0.5, 0.28, 0.38, 1.0};
  317.     static float back_mat_shininess[] = {60.0};
  318.     static float back_mat_specular[] = {0.5, 0.5, 0.2, 1.0};
  319.     static float back_mat_diffuse[] = {1.0, 1.0, 0.2, 1.0};
  320.     static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
  321.     static float lmodel_twoside[] = {GL_TRUE};
  322.  
  323.     glMatrixMode(GL_PROJECTION);
  324.     gluPerspective(450, 1.0, 0.5, 10.0);
  325.  
  326.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  327.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  328.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  329.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  330.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  331.     glEnable(GL_LIGHTING);
  332.     glEnable(GL_LIGHT0);
  333.     
  334.     glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
  335.     glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
  336.     glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  337.     glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
  338.     glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  339.     glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  340.     if (rgb) {
  341.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  342.     }
  343.  
  344.     if (rgb) {
  345.     glEnable(GL_COLOR_MATERIAL);
  346.     } else {
  347.     SetColorMap();
  348.     }
  349. }
  350.  
  351. static void InitTexture(void)
  352. {
  353.  
  354.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  355.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  356.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  357.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  358.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  359. }
  360.  
  361. static void Init(void)
  362. {
  363.  
  364.     glClearColor(0.0, 0.0, 0.0, 0.0);
  365.  
  366.     glShadeModel(GL_FLAT);
  367.     
  368.     glFrontFace(GL_CW);
  369.  
  370.     glEnable(GL_DEPTH_TEST);
  371.  
  372.     InitMaterials();
  373.     InitTexture();
  374.     InitMesh();
  375.  
  376.     glMatrixMode(GL_MODELVIEW);
  377.     glTranslatef(0.0, 0.4, -1.8);
  378.     glScalef(2.0, 2.0, 2.0);
  379.     glRotatef(-35.0, 1.0, 0.0, 0.0);
  380.     glRotatef(35.0, 0.0, 0.0, 1.0);
  381. }
  382.  
  383. static void Reshape(int width, int height)
  384. {
  385.  
  386.     glViewport(0, 0, (GLint)width, (GLint)height);
  387. }
  388.  
  389. static GLenum Key(int key, GLenum mask)
  390. {
  391.  
  392.     switch (key) {
  393.       case TK_ESCAPE:
  394.     tkQuit();
  395.       case TK_c:
  396.     contouring++;
  397.     if (contouring == 1) {
  398.         static GLfloat map[4] = {0, 0, 20, 0};
  399.  
  400.         glTexImage2D(GL_TEXTURE_2D, 0, 1, 4, 4, 0, GL_LUMINANCE,
  401.              GL_UNSIGNED_BYTE, (GLvoid *)contourTexture1);
  402.         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  403.         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  404.         glTexGenfv(GL_S, GL_OBJECT_PLANE, map);
  405.         glTexGenfv(GL_T, GL_OBJECT_PLANE, map);
  406.         glEnable(GL_TEXTURE_2D);
  407.         glEnable(GL_TEXTURE_GEN_S);
  408.         glEnable(GL_TEXTURE_GEN_T);
  409.     } else if (contouring == 2) {
  410.         static GLfloat map[4] = {0, 0, 20, 0};
  411.  
  412.         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  413.         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  414.         glPushMatrix();
  415.         glMatrixMode(GL_MODELVIEW);
  416.         glLoadIdentity();
  417.         glTexGenfv(GL_S, GL_EYE_PLANE, map);
  418.         glTexGenfv(GL_T, GL_EYE_PLANE, map);
  419.         glPopMatrix();
  420.     } else {
  421.         contouring = 0;
  422.         glDisable(GL_TEXTURE_GEN_S);
  423.         glDisable(GL_TEXTURE_GEN_T);
  424.         glDisable(GL_TEXTURE_2D);
  425.     }
  426.     break;
  427.       case TK_s:
  428.     smooth = !smooth;
  429.     if (smooth) {
  430.         glShadeModel(GL_SMOOTH);
  431.     } else {
  432.         glShadeModel(GL_FLAT);
  433.     }
  434.     break;
  435.       case TK_l:
  436.     lighting = !lighting;
  437.     if (lighting) {
  438.         glEnable(GL_LIGHTING);
  439.         glEnable(GL_LIGHT0);
  440.         if (rgb) {
  441.         glEnable(GL_COLOR_MATERIAL);
  442.         }
  443.     } else {
  444.         glDisable(GL_LIGHTING);
  445.         glDisable(GL_LIGHT0);
  446.         if (rgb) {
  447.         glDisable(GL_COLOR_MATERIAL);
  448.         }
  449.     }
  450.     break;
  451.       case TK_d:
  452.     depth = !depth;
  453.     if (depth) {
  454.         glEnable(GL_DEPTH_TEST);
  455.         clearMask |= GL_DEPTH_BUFFER_BIT;
  456.     } else {
  457.         glDisable(GL_DEPTH_TEST);
  458.         clearMask &= ~GL_DEPTH_BUFFER_BIT;
  459.     }
  460.     break;
  461.       case TK_SPACE:
  462.     stepMode = !stepMode;
  463.     if (stepMode) {
  464.         tkIdleFunc(0);
  465.         tkDisplayFunc(Animate);
  466.     } else {
  467.         tkIdleFunc(Animate);
  468.         tkDisplayFunc(0);
  469.     }
  470.     break;
  471.       case TK_n:
  472.     if (stepMode) {
  473.         nextFrame = 1;
  474.     }
  475.     break;
  476.       case TK_a:
  477.     spinMode = !spinMode;
  478.     break;
  479.       default:
  480.     return GL_FALSE;
  481.     }
  482.     return GL_TRUE;
  483. }
  484.  
  485. static GLenum Args(int argc, char **argv)
  486. {
  487.     GLint i;
  488.  
  489.     rgb = GL_TRUE;
  490.     doubleBuffer = GL_FALSE;
  491.     directRender = GL_TRUE;
  492.     frames = 10;
  493.     widthX = 10;
  494.     widthY = 10;
  495.     checkerSize = 2;
  496.     height = 0.2;
  497.  
  498.     for (i = 1; i < argc; i++) {
  499.     if (strcmp(argv[i], "-ci") == 0) {
  500.         rgb = GL_FALSE;
  501.     } else if (strcmp(argv[i], "-rgb") == 0) {
  502.         rgb = GL_TRUE;
  503.     } else if (strcmp(argv[i], "-sb") == 0) {
  504.         doubleBuffer = GL_FALSE;
  505.     } else if (strcmp(argv[i], "-db") == 0) {
  506.         doubleBuffer = GL_TRUE;
  507.     } else if (strcmp(argv[i], "-dr") == 0) {
  508.         directRender = GL_TRUE;
  509.     } else if (strcmp(argv[i], "-ir") == 0) {
  510.         directRender = GL_FALSE;
  511.     } else if (strcmp(argv[i], "-grid") == 0) {
  512.         if (i+2 >= argc || argv[i+1][0] == '-' || argv[i+2][0] == '-') {
  513.         printf("-grid (No numbers).\n");
  514.         return GL_FALSE;
  515.         } else {
  516.         widthX = atoi(argv[++i]);
  517.         widthY = atoi(argv[++i]);
  518.         }
  519.     } else if (strcmp(argv[i], "-size") == 0) {
  520.         if (i+1 >= argc || argv[i+1][0] == '-') {
  521.         printf("-checker (No number).\n");
  522.         return GL_FALSE;
  523.         } else {
  524.         checkerSize = atoi(argv[++i]);
  525.         }
  526.     } else if (strcmp(argv[i], "-wave") == 0) {
  527.         if (i+1 >= argc || argv[i+1][0] == '-') {
  528.         printf("-wave (No number).\n");
  529.         return GL_FALSE;
  530.         } else {
  531.         height = atof(argv[++i]);
  532.         }
  533.     } else if (strcmp(argv[i], "-frames") == 0) {
  534.         if (i+1 >= argc || argv[i+1][0] == '-') {
  535.         printf("-frames (No number).\n");
  536.         return GL_FALSE;
  537.         } else {
  538.         frames = atoi(argv[++i]);
  539.         }
  540.     } else {
  541.         printf("%s (Bad option).\n", argv[i]);
  542.         return GL_FALSE;
  543.     }
  544.     }
  545.     return GL_TRUE;
  546. }
  547.  
  548. void main(int argc, char **argv)
  549. {
  550.     GLenum type;
  551.  
  552.     if (Args(argc, argv) == GL_FALSE) {
  553.     tkQuit();
  554.     }
  555.  
  556.     tkInitPosition(0, 0, 300, 300);
  557.  
  558.     type = TK_DEPTH;
  559.     type |= (rgb) ? TK_RGB : TK_INDEX;
  560.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  561.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  562.     tkInitDisplayMode(type);
  563.  
  564.     if (tkInitWindow("Wave Demo") == GL_FALSE) {
  565.     tkQuit();
  566.     }
  567.  
  568.     Init();
  569.  
  570.     tkExposeFunc(Reshape);
  571.     tkReshapeFunc(Reshape);
  572.     tkKeyDownFunc(Key);
  573.     tkIdleFunc(Animate);
  574.     tkExec();
  575. }
  576.